import { createRouter, createWebHistory } from 'vue-router';
import { useAccountStore } from '@/stores/account.js';
import Login from '@/views/Login.vue';
import Main from '@/views/Main.vue';
import Inbound from '@/views/Inbound.vue';
import Outbound from '@/views/Outbound.vue';
import Product from '@/views/Product.vue';
import EditProduct from '@/views/EditProduct.vue';
import Statistics from '@/views/Statistics.vue';
import EditStatistics from '@/views/EditStatistics.vue';
import EditInbound from '@/views/EditInbound.vue';
import EditOutbound from '@/views/EditOutbound.vue';
import Forewarning from '@/views/Forewarning.vue';
import EditForewarning from '@/views/EditForewarning.vue';
import Account from '@/views/Account.vue';
import EditAccount from '@/views/EditAccount.vue';
import NotFound from '@/views/NotFound.vue';
import Vendor from '@/views/Vendor.vue';
import Recipient from '@/views/Recipient.vue';
import EditVendor from '@/views/EditVendor.vue';
import EditRecipient from '@/views/EditRecipient.vue';
import Earthworks from '@/views/Earthworks.vue';
import EditEarthworks from '@/views/EditEarthworks.vue';
import Program from '@/views/Program.vue';
import EditProgram from '@/views/EditProgram.vue';
import Items from '@/views/Items.vue';
import EditItems from '@/views/EditItems.vue';
const routes = [
	{ path: '/', component: Login },
	{
		path: '/main',
		component: Main,
		children: [
			{
				path: 'inbound',
				component: Inbound
			},
			{
				path: 'inbound/edit',
				component: EditInbound
			},
			{ path: 'outbound', component: Outbound },
			{ path: 'outbound/edit', component: EditOutbound },
			{ path: 'product', component: Product },
			{
				path: 'product/edit',
				component: EditProduct
			},
			{ path: 'statistics', component: Statistics },
			{ path: 'statistics/save', component: EditStatistics },
			{ path: 'forewarning', component: Forewarning },
			{ path: 'forewarning/edit', component: EditForewarning },
			{ path: 'account', component: Account },
			{ path: 'account/edit', component: EditAccount },
			{ path: 'vendor', component: Vendor },
			{ path: 'vendor/edit', component: EditVendor },
			{ path: 'recipient', component: Recipient },
			{ path: 'recipient/edit', component: EditRecipient },
			{ path: 'earthwork', component: Earthworks },
			{ path: 'earthwork/edit', component: EditEarthworks },
			{ path: 'program', component: Program },
			{ path: 'program/edit', component: EditProgram },
			{ path: 'items', component: Items },
			{ path: 'items/edit', component: EditItems }
		]
	},
	{ path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFound }
];

const router = createRouter({
	history: createWebHistory(),
	routes
});
router.beforeEach((to, from, next) => {
	const account = useAccountStore();
	if (account.selectedKeys.length !== 0) {
		next();
	} else {
		if (to.path.includes(from.path)) {
			account.selectedKeys = [from.path];
		} else {
			account.selectedKeys = [to.path];
		}
		next();
	}
});
export default router;
